package org.example;




class Solution {
    static int countBit(int n) {
        int count = 0;
        while (n > 0) {
            n = n & (n - 1);
            count++;
        }
        return count;
    }

    public int[] sortByBits(int[] arr) {
        List<Integer> arrList = new ArrayList<>(arr.length);
        for (int v : arr) {
            arrList.add(v);
        }
        arrList.sort((v1, v2) -> {
            int res = Integer.compare(countBit(v1), countBit(v2));
            if (res == 0) {
                res = Integer.compare(v1, v2);
            }
            return res;
        });
        // Collections.sort(arrList, (v1, v2) -> {
        //     int res = Integer.compare(countBit(v1), countBit(v2));
        //     if (res == 0) {
        //         res = Integer.compare(v1, v2);
        //     }
        //     return res;
        // });
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arrList.get(i);
        }
        return arr;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}